package neusoft.edu.cn.wuye.Activities;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;

import java.util.ArrayList;

import neusoft.edu.cn.wuye.Fragments.MessageFragment;
import neusoft.edu.cn.wuye.Fragments.NewsFragment;
import neusoft.edu.cn.wuye.Fragments.ServiceFragment;
import neusoft.edu.cn.wuye.R;

public class MainActivity extends AppCompatActivity {
    private static final String TAG="MainActivity";

    ViewPager viewPager;
    FragmentPagerAdapter adapter;
    BottomNavigationView bottomNavigationView;
    ArrayList<Fragment> fragmentArrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();

        fragmentArrayList = new ArrayList<>();
        fragmentArrayList.add(new NewsFragment());
        fragmentArrayList.add(new MessageFragment());
        fragmentArrayList.add(new ServiceFragment());

        Log.v(TAG,""+fragmentArrayList.size());

        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragmentArrayList.get(i);
            }

            @Override
            public int getCount() {
                return fragmentArrayList.size();
            }
        };
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                int id=0;
                switch (i){
                    case 0:
                        id=R.id.nav_news;
                        break;
                    case 1:
                        id=R.id.nav_message;
                        break;
                    case 2:
                        id=R.id.nav_service;
                        break;
                }
                bottomNavigationView.setSelectedItemId(id);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                int id=0;
                switch (menuItem.getItemId()){
                    case R.id.nav_news:
                        id=0;
                        break;
                    case R.id.nav_message:
                        id=1;
                        break;
                    case R.id.nav_service:
                        id=2;
                        break;
                }
                viewPager.setCurrentItem(id);
                return true;
            }
        });

        bottomNavigationView.setSelectedItemId(R.id.nav_news);

    }

    private void init() {
        viewPager = findViewById(R.id.viewPager);
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
    }
}
